<!DOCTYPE HTML>
<html th:with="title='编辑权限'" >
<head th:include="fragments/header :: header">
</head>
<body>
<div class="page-container">
	<form action="/security/role/save" method="post" class="form form-horizontal" id="role-form">
		<input type="hidden" name="id" v-model="id" />
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">上级权限：</label>
			<div class="formControls col-xs-7 col-sm-8">
				<input type="text" class="input-text size-S" v-model="parent.remark" id="parentName" name="parentName" readonly="readonly">
				<input type="hidden" id="parentId" name="parentId" v-model="parent.id">
			</div>
			<div class="formControls col-xs-1 col-sm-1">
				<input class="btn btn-link" type="button"  onclick="showMenu();" value="选择">
			</div>			
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>名称：</label>
			<div class="formControls col-xs-7 col-sm-8">
				<input type="text" v-model="name" class="input-text size-S" id="name" name="name" onkeyup="this.value = this.value.toUpperCase();">
				<span class="c-red">以ROLE_开头，包含英文、数字、下划线，例：ROLE_SECURITY_USER_ADD</span>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">描述：</label>
			<div class="formControls col-xs-7 col-sm-8">
				<input type="text" v-model="remark" class="input-text size-S" id="remark" name="remark">
			</div>
		</div>
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
				<input class="btn btn-primary size-L radius" type="submit" value="提交">
				<input class="btn btn-primary size-L radius" type="button" onclick="closeWin()" value="关闭">
			</div>
		</div>
	</form>
</div>
<div th:include="fragments/widget :: singleSelectRoleTreePanel">
</div>
<script th:include="fragments/widget :: singleSelectRoleTreeJs" type="text/javascript">
</script>
<script type="text/javascript">
function closeWin() {
	var index = parent.layer.getFrameIndex(window.name);
	parent.layer.close(index);
}
function resetForm() {
	$('#role-form')[0].reset();
}

function getRoleId() {
	var addr = location.href;
	var index = addr.lastIndexOf('/') + 1;
	var id = addr.substr(index);
	return id;
}

function getRole() {
	var id = getRoleId();
	$.ajax({
	    type: "GET",
	    url: '/security/role/get/'+id,
	    beforeSend: function () {
	    },
	    complete: function () {
	    },
	    error: function(request) {
	        alert("Connection error");
	    },
	    success: function(result) {
	        if (result.status == 0) {
	        	var data = result.data;
	        	new Vue({
	        		  el: '#role-form',
	        		  data: data
	        		});
	        }
	    }
	});
}

$(function(){	
	getRole();
	$("#role-form").validate({
		rules:{
			name:{
				required:true,
				minlength:6,
				maxlength:200
			},
			remark:{
				required:true,
				minlength:2,
				maxlength:200
			}
		},
		onkeyup:false,
		focusCleanup:true,
		success:"valid",
		submitHandler:function(form){
			var data = $('#role-form').serialize();
			if ($("#parentId").val() != "") {
				data += "&parent.id=" + $("#parentId").val();
			}
			$.ajax({
			    type: "POST",
			    url: '/security/role/update',
			    data: data,
			    beforeSend: function () {
			    	
			    },
			    complete: function () {
			    	
			    },
			    error: function(request) {
			    	layer.alert("系统出错");
			    },
			    success: function(data) {
			    	if (data.status == 0) {
			    		refreshUParentList();
						layer.alert(data.message, function() {
							var index = parent.layer.getFrameIndex(window.name);
							parent.layer.close(index);
						});
					}else{
						layer.alert(data.message);
					}
			    }
			});
		}
	});
});
</script> 
</body>
</html>